
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const input = [];
len [n, m] = [0, 0];
let graph = [];
let visited;
let ans = 0;
let dx = [0, 1, 0, -1];
let dy = [1, 0, -1, 0];

rl.on('line', (line) => {
  input.push(line);
}).on('close', () => {
  processInput(input);
  console.log(graph);

  for (let i = 0; i < n; i++) {
    for (let j = 0; j < m; j++) {
      if (!visited[i][j] && graph[i][j] === 1) {
        dfs(graph, [i, j]);
        ans++;
      }
    }
  }
  console.log(ans);
});

function processInput(input) {
  [n, m] = input[0].split(' ').map(Number);
  for (let i = 1; i <= n; i++) {
    let row = input[i].split(' ').map(Number);
    graph.push([...row]);
  }
  visited = new Array(n).fill(false).map(() => new Array(m).fill(false));
}


function dfs(graph, start) {
  visited[start[0]][start[1]] = true;
  for (let i = 0; i < 4; i++) {
    let [x, y] = [start[0] + dx[i], start[1] + dy[i]];
    if (x >= 0 && x < n && y >= 0 && y < m && !visited[x][y] && graph[x][y] === 1) {
      dfs(graph, [x, y], visited);
    }
  }
}

